package com.clock.scratch.factory;

import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.InputStream;

public class PixelMapFactory {

    /**
     * 通过图片资源ID获取位图对象
     * @param context Context
     * @param resId 资源 id
     * @return 位图
     */
    public static PixelMap createPixelMap(Context context, int resId){

        return createPixelMap(context, resId, 0, 0);
    }

    /**
     * 通过图片资源ID获取位图对象
     * @param context Context
     * @param resId 资源 id
     * @param width icon width
     * @param height icon height
     * @return 位图
     */
    public static PixelMap createPixelMap(Context context, int resId, int width, int height){
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = context.getResourceManager().getResource(resId);
            ImageSource imageSource = ImageSource.create(drawableInputStream, null);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            if (width>0&&height>0)decodingOptions.desiredSize = new Size(width, height);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if (drawableInputStream != null){
                    drawableInputStream.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 根据位图创建位图
     * @param pixelMap
     * @param width
     * @param height
     * @return
     */
    public static PixelMap createPixelMap(PixelMap pixelMap, int width, int height){
        try {
            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.releaseSource = false;
            options.pixelFormat = PixelFormat.ARGB_8888;
            options.size = new Size(width, height);
            return PixelMap.create(pixelMap,options);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据颜色创建位图
     * @param color
     * @param width
     * @param height
     * @return
     */
    public static PixelMap createPixelMap(int color, int width, int height){
        try {
            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.releaseSource = false;
            options.pixelFormat = PixelFormat.ARGB_8888;
            options.size = new Size(width, height);
            int[] colors = new int[width*height];
            for (int i=0;i<colors.length;i++)colors[i]=color;
            return PixelMap.create(colors,options);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



}